/**   
* @Title: CategoryService.java 
* @Package com.csis.system.service 
* @Description: TODO
* @author dailey  
* @date 2012-10-6
* @version V1.0   
*/
package com.csis.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csis.framework.db.BaseDao;
import com.csis.framework.domain.Condition;
import com.csis.system.common.IStaticData;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.Category;
import com.csis.system.domain.CategoryFormBean;

/**
 * @author dailey
 *
 */
@Service("categoryService")
public class CategoryService {

	private Log log = LogFactory.getLog(getClass());

	@Resource
	private BaseDao baseDao;

	public Category load(Integer categoryId) {
		Category category = baseDao.load(Category.class, categoryId);
		return category;
	}

	public List<Category> getAllCategorys() {
		List<Category> categories = null;
		String sql = "SELECT * FROM " + IStaticData.TABLE_CATEGORY;
		categories = baseDao.query4SingleTable(Category.class, sql);
		return categories;
	}

	public List<Category> getCategorysByParent(Integer id) {
		List<Category> categories = null;
		String sql = "SELECT * FROM " + IStaticData.TABLE_CATEGORY + " WHERE "
				+ IStaticData.COLUMN_CATEGORY_CATEGORYPARENT + "=?";
		categories = baseDao.query4SingleTable(Category.class, sql, id);
		return categories;
	}

	public CategoryFormBean list(CategoryFormBean formBean) {
		String sql = getSearchSql(formBean);

		Condition condition = formBean.getCondition();
		if (condition != null) {
			sql = condition.getCondition(sql);
		}
		// importance
		int maxCount = getMaxCount(formBean);
		formBean.getPage().setMaxCount(maxCount);
		sql = formBean.getPage().perPageSql(sql);
		List<Category> beans = baseDao.query4SingleTable(Category.class, sql);
		formBean.setBeans(beans);
		return formBean;
	}

	/**
	 * @return
	 */
	public int getMaxCount(CategoryFormBean formBean) {
		String sql = getSearchSql(formBean);
		return baseDao.getMaxCount(sql);
	}

	/**
	 * @param formBean
	 * @return
	 */
	private String getSearchSql(CategoryFormBean formBean) {
		String sql = "SELECT * FROM " + IStaticData.TABLE_CATEGORY + " WHERE 1=1 ";
		return sql;
	}

	@Transactional
	public void saveCategory(Category category) {
		log.info("CategoryService>>SaveCategory>>" + category);
		if (category.getId() == null) {
			baseDao.persist(category);
		} else {
			baseDao.merge(category);
		}
	}

	@Transactional
	public void removeCategory(Integer id) {
		log.info("CategoryService>>RemoveCategory>>" + id);
		baseDao.remove(Category.class, id);
	}

	@Transactional
	public void remove(ConvertArray<Integer> categoryIds) {
		if (categoryIds == null) {
			return;
		}
		for (Integer id : categoryIds) {
			removeCategory(id);
		}
	}

	/**
	 * @param name
	 * @return
	 */
	public List<Category> getCategorysByName(String name) {
		return baseDao.query4SingleTable(Category.class, "SELECT * FROM " + IStaticData.TABLE_CATEGORY + " WHERE "
				+ IStaticData.COLUMN_CATEGORY_CATEGORYNAME + " = ?", name);
	}

}
